package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.MD5Utils;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.*;

@BusinessDataConverter(
    auto = false,

    updateSql = "UPDATE ACT_HI_IDENTITYLINK\n" +
        "SET GROUP_ID_=?, TYPE_=?, USER_ID_=?, TASK_ID_=?, PROC_INST_ID_=?\n" +
        "WHERE ID_= ?",

    insertSql =  "INSERT INTO ACT_HI_IDENTITYLINK\n" +
        "(ID_, GROUP_ID_, TYPE_, USER_ID_, TASK_ID_, PROC_INST_ID_)\n" +
        "VALUES(?,?,?,?,?,?)",

    findMigratedIdListSql = "SELECT ID_ FROM ACT_HI_IDENTITYLINK WHERE PROC_INST_ID_ = ? OR TASK_ID_ IN (SELECT ID_ FROM ACT_HI_TASKINST WHERE PROC_INST_ID_ = ? )"
)
public class ActHiIdentitylink extends AbstractWorkflowBusiness {

  public ActHiIdentitylink(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId);
  }

  @Override
  public void findMigratedIdList() throws SQLException {
    this.migratedIdList = this.targetDbUtil.selectSingleColumn(findMigratedIdListSql, Arrays.asList(this.procInstId+"-JW",this.procInstId+"-JW"));
  }

  public void migrate() {

  }

  public void customMigrate(Map<String,Map<String,String>> map) throws SQLException {
    for (Map.Entry<String, Map<String,String>> entry : map.entrySet()) {
      String key = entry.getKey();
      Map<String,String> value  = entry.getValue();
      for (Map.Entry<String, String> valueEntry : value.entrySet()) {
        String groupId = null;
        String type = valueEntry.getKey();
        String userIds = valueEntry.getValue();
        String taskId = key.equals("process") ? null : key + "-JW";
        String[] userIdArr = userIds.split(",");

        TreeSet<String> userIdTreeSet = new TreeSet<>();
        for(String s : userIdArr) {
          userIdTreeSet.add(s);
        }
        for(String userId: userIdTreeSet) {
          int needMigratedResult = this.needMigrated(MD5Utils.stringToMD5(groupId + type + userId + taskId + procInstId));
          if (needMigratedResult == 0) {
            continue;
          }
          if (needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql, this.buildValues(groupId, type, userId, taskId), true);
          } else {
            this.targetDbUtil.updateByParams(insertSql, this.buildValues(groupId, type, userId, taskId), false);
          }
        }
      }
    }
  }

  public List buildValues(String groupId,String type,String userId,String taskId) throws SQLException {
    List values = new ArrayList();
//    ID_
    values.add(MD5Utils.stringToMD5(groupId + type + userId + taskId + procInstId));
//    GROUP_ID_
    values.add(groupId);
//    TYPE_
    values.add(type);
//    USER_ID_
    values.add(userId);
//    TASK_ID_
    values.add(taskId);
//    PROC_INST_ID_
    values.add(taskId == null ? this.procInstId + "-JW" : null);
    return values;
  }


}
